<?php

class Admin_CategoriesController extends Zend_Controller_Action
{
    protected $_categories;

    protected $_flashMessenger = null;

    public function init()
    {
        $this->_helper->layout()->setLayout('admin');
        $this->view->title = 'Блог-Редактирование категорий';
        $this->view->headTitle()->setSeparator(' | ');
        $this->view->headTitle($this->view->title);

        $this->_categories = new Application_Model_DbTable_Categories();
        $this->_flashMessenger = $this->_helper->FlashMessenger;
    }

    public function indexAction()
    {
        $this->view->categories = $this->_categories->getAllCategories();
        $this->view->parentCategory = $this->_categories->getParentCategories();
        $this->view->messages = $this->_flashMessenger->getMessages();
    }

    public function addCategoryAction()
    {
        $addForm = new Application_Form_AddCategory();
        $this->view->addForm = $addForm;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($addForm->isValid($formData)) {
                $catName    = $addForm->getValue('title');
                $catUrl     = $addForm->getValue('url');
                $parentCat  = $addForm->getValue('parent_id');
                $this->_categories->add($catName, $catUrl, $parentCat);
                $this->_flashMessenger->addMessage('Категория создана');
                $this->_redirect('aria77/categoryControl');
            } else {
                $this->message = "Валидация не пройдена!";
                $addForm->populate($formData);
            }
        }
    }

    public function editCategoryAction()
    {
        $id = $this->_getParam('id', 0);
        $editForm = new Application_Form_AddCategory();
        $this->view->editForm = $editForm;
        if ($this->getRequest()->isPost()) {
            if ($editForm->isValid($_POST)) {
                $catName    = $editForm->getValue('title');
                $catUrl     = $editForm->getValue('url');
                $parentCat  = $editForm->getValue('parent_id');
                $result = $this->_categories->edit($id, $catName, $catUrl, $parentCat);
                if($result){
                    $this->_flashMessenger->addMessage('Категория изменена');
                    $this->_redirect('aria77/categoryControl');
                } else {
                    $this->_flashMessenger->addMessage('Категория не может быть изменена');
                    $this->_redirect('aria77/categoryControl');
                }
            }
        } else {
            $data = $this->_categories->getById($id);
            if (!$data) {
                $this->_redirect('error/404');
            }
            $data = $data->toArray();
            $editForm->populate($data);
        }
    }

    public function deleteCategoryAction()
    {
        $id = $this->_getParam('id', 0);

        $data = $this->_categories->getById($id);
        $this->view->catName = $data;

        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('delete');
            if ($del == 'yes') {
                if ($this->_categories->deleteCategory($id)) {
                    $this->_flashMessenger->addMessage('Категория удалена');
                } else {
                    $this->_flashMessenger->addMessage('Категория не можеть быть удалена!');
                }
                $this->_redirect('/admin/categories');
            } else {
                $this->_redirect('/admin/categories');
            }
        }
    }
}




